#!/usr/bin/python3
# -*- coding:utf-8 -*-
""""
@Author: lijk34925
@Time: 2023/6/14 16:33
@File: MarketMethod.py
"""
import json
from datetime import datetime

from jsonpath import jsonpath

from BladeFerm.databases.DealSql.fields_map import FieldsMap
from BladeFerm.utils.jsonOperation import JsonOperation


class MarketMethod(FieldsMap):

    def add_filter_fnc(self):
        super().add_filter_fnc()
        self.env.filters['to_datatime'] = self.__str_to_datatime
        self.env.filters['write_json'] = self.__write_json
        self.env.filters['json.loads'] = self.__str_to_dict
        self.env.filters['jsonpath'] = self.__json_path


    @staticmethod
    def __str_to_datatime(t_str, time_format="%H:%M"):
        return datetime.strptime(t_str, time_format)

    @staticmethod
    def __write_json(filepath, jp='$..EPS', index=0):
        content = JsonOperation().read_json(filepath, encoding='utf-8')
        jp_res = jsonpath(content, jp)
        jp_res = jp_res[index] if jp_res else None
        return jp_res

    @staticmethod
    def __str_to_dict(value: str):
        return json.loads(value)

    @staticmethod
    def __json_path(data, jp='$..key', index=0):
        jp_res = jsonpath(data, jp)
        return jp_res[index] if jp_res else None